
--Sp_Get_Constante 1073,0,0,0,0,0,0,null

CREATE PROCEDURE [dbo].[Sp_Get_Constante]
    @nConCodigo INT  = NULL,
    @nConValor INT  = NULL,
    @ConLeft INT  = NULL,
	@ConValLeft INT  = NULL,
    @ConRight INT  = NULL,
	@ConValRight INT  = NULL,
    @NotIn VARCHAR(20) = NULL,
	@In VARCHAR(20) = NULL
AS 
    BEGIN

		DECLARE @sql AS VARCHAR (MAX)      

		SET @sql = 'SELECT  nConValor, cConDescripcion, nConCodigo FROM    Constante
        WHERE   ( nConCodigo = ' + CAST(@nConCodigo AS NVARCHAR(10)) + ' ) '

		if @nConValor > 0
			begin
				set @sql = @sql + ' AND ( LEN(nConValor) = ' + CAST(@nConValor AS NVARCHAR(10)) + ' ) '
			end	

		IF @ConLeft > 0
			BEGIN
				SET @Sql = @Sql + ' AND (LEFT(nConValor, ' + CAST(@ConLeft AS NVARCHAR(10)) + ') = ' + CAST(@ConValLeft AS NVARCHAR(10)) +  ' )'
			END          
		IF @ConRight > 0
			BEGIN
				SET @Sql = @Sql + ' AND (RIGHT(nConValor, ' + cast(@ConRight AS NVARCHAR(10)) + ') = ' + CAST(@ConValRight AS NVARCHAR(10)) +  ' )'
			END   


		IF LEN(@NotIn)> 0 
			BEGIN
			    SET @sql = @sql + ' AND nConValor NOT IN ('  + @NotIn +  ')'     
			END          

		IF LEN(@In) > 0
			BEGIN
				SET @sql = @sql + ' AND nConValor IN ('  + @In +  ')'
			END          

		exec(@sql)

    END
	 